package com.logic.modular.game.shop.service.impl;

import com.comment.exception.ServiceException;
import com.logic.modular.game.shop.domain.PlayerShop;
import com.logic.modular.game.shop.service.PlayerShopService;
import com.logic.modular.game.player.bag.service.BagItemServiceImpl;
import com.logic.modular.game.player.bag.service.PlayerPropType1ServiceImpl;
import com.comment.pojo.domain.logic.core.CoreConfig;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import com.comment.pojo.domain.logic.core.CoreShopGoods;
import com.logic.modular.gameCore.shop.service.CoreShopGoodsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 玩家(Shop)服务实现类
 */
@Service
@Slf4j
public class PlayerShopServiceImpl implements PlayerShopService {
    @Resource
    private PlayerPropType1ServiceImpl playerPropType1Service;
    @Resource
    private BagItemServiceImpl bagItemService;

    @Override
    public @NotNull PlayerShop getGoodsList(Long playerId) {
        PlayerShop playerShop = PlayerShop.get(playerId);
        if (playerShop == null || CoreShopGoodsServiceImpl.checkSize(playerShop.getGoodsMap().values().size())) {
            playerShop = new PlayerShop();
            playerShop.setPlayerId(playerId);
            List<CoreShopGoods> collect = CoreShopGoodsServiceImpl.listCache().stream()
                    .filter(item -> item.getType() != 4)
                    .collect(Collectors.toList());
            collect.addAll(CoreShopGoodsServiceImpl.obtainEquip());
            Map<String, CoreShopGoods> playerGoodsMap = collect.stream()
                    .collect(Collectors.toMap(item -> item.getId().toString(), item -> item));
            playerShop.setGoodsMap(playerGoodsMap);
            playerShop.setEquipLastRefreshTime(System.currentTimeMillis());
            playerShop.setEquipRefreshCount(0);
            playerShop.save();
        }
        return playerShop;
    }

    @Override
    public PlayerShop flashEquipPool(Long playerId) {
        PlayerShop playerShop = PlayerShop.get(playerId);
        if (playerShop == null) {
            throw new ServiceException("请先获取商品列表");
        }

        CoreConfig coreConfig = CoreConfigServiceImpl.getCache(11L);
        BigDecimal value1 = coreConfig.getValue1();
        int intValue2 = Integer.parseInt(coreConfig.getValue2());
        // int intValue3 = Integer.parseInt(coreConfig.getValue3());

        int equipRefreshCount = playerShop.getEquipRefreshCount();
        if (equipRefreshCount > intValue2) {
            throw new ServiceException("今日刷新次数已用完");
        }
        // 付费
        BigDecimal needPay = value1.multiply(new BigDecimal(equipRefreshCount + 1));
        boolean b = playerPropType1Service.tryPay(playerId, 1L, needPay);
        if (!b) {
            throw new ServiceException("元宝不足");
        }
        // 玩家商店数据
        playerShop.refreshEquip();
        playerShop.setEquipRefreshCount(equipRefreshCount + 1);
        playerShop.save();
        return playerShop;
    }

    @Override
    public @NotNull PlayerShop buy(Long playerId, String goodsIdStr, Integer buyCount) {
        PlayerShop playerShop = PlayerShop.get(playerId);
        if (playerShop == null) {
            throw new ServiceException("请先获取商品列表");
        }
        Map<String, CoreShopGoods> playerShopGoodsMap = playerShop.getGoodsMap();
        CoreShopGoods playerShopGoods = playerShopGoodsMap.get(goodsIdStr);
        if (playerShopGoods == null) {
            throw new ServiceException("商品不存在");
        }

        if (playerShopGoods.getLimitType() != 0) {
            int targetNum = playerShopGoods.getBuyNum() + buyCount;
            if (targetNum > playerShopGoods.getLimitNum()) {
                throw new ServiceException("库存不足");
            }
        }

        AtomicBoolean isPay = new AtomicBoolean(false);
        Map<Long, Integer> price = playerShopGoods.getPrice();

        Map<Long, BigDecimal> costBackLog = new HashMap<>();
        price.forEach((key, value) -> {
            BigDecimal bigDecimal = new BigDecimal(value * buyCount);
            isPay.set(playerPropType1Service.tryPay(playerId, key, bigDecimal));
            if (isPay.get()) {
                costBackLog.put(key, bigDecimal);
                // 获取道具
                Long propId = playerShopGoods.getPropId();
                bagItemService.obtainBagItem(playerId, propId, new BigDecimal(buyCount));
                // 更新限购
                playerShopGoods.setBuyNum(playerShopGoods.getBuyNum() + buyCount);
                playerShopGoodsMap.put(goodsIdStr, playerShopGoods);
                playerShop.setGoodsMap(playerShopGoodsMap);
                playerShop.save();
            } else {
                costBackLog.forEach((k, v) -> bagItemService.obtainBagItem(playerId, k, v));
            }
        });
        return playerShop;
    }
}
